package meterMonitor.controller;


import meterMonitor.biz.AreaBiz;
import meterMonitor.consts.ResponseCode;
import meterMonitor.framework.auth.AuthorityType;
import meterMonitor.framework.auth.FireAuthority;
import meterMonitor.model.Area;
import meterMonitor.vo.BaseResponseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

@RequestMapping("areaManger")
@RestController
public class AreaController {
    @Autowired
    AreaBiz areaBiz;
    public static final Logger logger = LoggerFactory.getLogger(WaterChartController.class);


    /**
     * @return 所有区域
     */
    @RequestMapping("areaAll.do")
    public List<Area> getAreaAll() {
        List<Area> areas = new LinkedList<>();
        areas = areaBiz.getfandAllArea(0);
        return areas;
    }

    /**
     * 删除某个区域
     *
     * @param area
     */
    @RequestMapping("delArea.do")
    public BaseResponseData delArea(Area area) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            areaBiz.delArea(area.getAreaId());
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("删除成功！");
        } catch (Exception e) {
            logger.error("更新区域信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("删除域信息失败！");
        }
        return baseResponseData;
    }

    /**
     * 修改某一区域
     *
     * @param area 区域类
     */
    @RequestMapping("updateArea.do")
    public BaseResponseData updateArea(Area area) {
        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            areaBiz.updateArea(area);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("更新成功！");
        } catch (Exception e) {
            logger.error("更新区域信息异常！", e);
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage("更新区域信息失败！");
        }
        return baseResponseData;
    }

    /**
     * 新增区域
     *
     * @return
     */
    @RequestMapping("insertArea.do")
    @FireAuthority(authorityTypes = AuthorityType.Edit)
    @ResponseBody
    public BaseResponseData insertArea(String areaName, String descrition) {

        BaseResponseData baseResponseData = new BaseResponseData();
        try {
            Area area = new Area();

            //查询所以对象
         Area areaid = areaBiz.getFindArea(areaName);
         if (areaid==null){
             System.out.println("kong");
         }else if (areaName.equals(areaid.getAreaName())) {

             baseResponseData.setMessage("区域名称已存在，请重新输入！");
             baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
             return baseResponseData;
         }


            area.setAreaName(areaName);
            area.setDescrition(descrition);
            areaBiz.insertArea(area);
            baseResponseData.setCode(ResponseCode.SUCCESS);
            baseResponseData.setMessage("区域添加成功！");
            return baseResponseData;
        } catch (Exception e) {
            logger.info(e.getMessage());
            baseResponseData.setCode(ResponseCode.DATA_EXCEPTION);
            baseResponseData.setMessage(e.getMessage());
        }
        return baseResponseData;
    }

}
